package com.forum.controller;

import com.forum.pojo.Module;
import com.forum.pojo.User;
import com.forum.service.MessageService;
import com.forum.service.UserService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.util.List;


@Controller
@RequestMapping("/user")
public class UserController {

    @Autowired
    private UserService userService;
    @Autowired
    private MessageService messageService;
    //注册，添加用户
    @RequestMapping(value = "/add",produces = "text/html;charset=UTF-8")
    public String save(User user, Model model){
        if(userService.findUserById(user.getUserID())!=null){
            model.addAttribute("registError","已存在账号");
            return "regist";
        }
        //注册：
        userService.addUser(user);
        return "login_test";
    }




    //登录
    @RequestMapping(value = "/login",produces = "text/html;charset=UTF-8")
    public String login(HttpServletResponse response, User user, HttpSession session, Model model){
        if(userService.findUserById(user.getUserID())==null){
            model.addAttribute("loginError","账号错误");
            return "login_test";
        }
        User u = userService.UserLogin(user);
        List<Module> moduleList = messageService.Allmodule();
        for (Module m:moduleList){
            System.out.println(m.getModuleName());
        }
        if(u!=null){
            //登录成功
            System.out.println("登录成功");
            session.setAttribute("sessionUser",u);
            session.setAttribute("moduleList",moduleList);
            return "index";
        }
        //密码错误登录失败
        model.addAttribute("loginError","密码错误");
        return "login_test";
    }


    //查找所有用户
    @RequestMapping(value = "/allUser",produces = "text/html;charset=UTF-8")
    public String allUser(@RequestParam(value="pageNum",required = false,defaultValue = "0")int pageNum, Model model){
        if(pageNum!=0){
            PageHelper.startPage(pageNum,2);
        }
        List<User> userList = userService.findAllUser();
//        PageInfo<User> pageInfo = new PageInfo<User>(userList);
//        System.out.println(pageInfo.getPageNum());

        //System.out.println(userList);
        model.addAttribute("userlist",userList);
        for(User u:userList){
            System.out.println(u);
        }
        return "";
    }

    //
    @RequestMapping(value = "/updateUser",produces = "text/html;charset=UTF-8")
    public String updateUser(User user, Model model){
        if(userService.updateUser(user)==1){
            model.addAttribute("userdata",user);
            return "index";
        }

        return "update";
    }

    //
    @RequestMapping(value = "/deleteUser",produces = "text/html;charset=UTF-8")
    public String deleteUser(User user, Model model){
        if(userService.deleteUser(user.getUserID())==1){
            //删除成功
            return "";
        }

        return "";
    }

    @RequestMapping(value = "/findUserByID",produces = "text/html;charset=UTF-8")
    public String findUserByID(String userID, Model model){
        User u = userService.findUserById(userID);
        model.addAttribute("userdata",u);
        return "PersonInfo";
    }

    @RequestMapping(value = "/outLogin",produces = "text/html;charset=UTF-8")
    public String outLogin(HttpSession session){
        session.invalidate();
        return "index";
    }
}
